﻿var PlayLayer = cc.Layer.extend({
    level: 0,
    score: 0,
    steps: 0,
    limitStep: 0,
    targetScore: 0,
    candySprite: null,
    ctor: function () {
        this._super();
        this._init();
        var size = cc.director.getWinSize();
        this.candySprite = [];
        for (var i = 0; i < Constant.CANDY_MAXSIZE; ++i) {
            var column = [];
            for (var j = 0; j < Constant.CANDY_MAXSIZE; ++j) {
                var item = Candy.createCandy(i, j);
                item.x = 32 + 64 * i;
                item.y = 32 + 64 * j;
                this.addChild(item);
                column.push(item);
            }
            this.candySprite.push(column);
        }
        this._addEvent();
    },
    _init: function () {
        this.level = 1;
        this.score = 0;
        this.steps = 0;
        this.limitStep = 30;
        this.targetScore = 500;
    },
    _addEvent: function () {
        cc.eventManager.addListener({
            event: cc.EventListener.MOUSE,
            onMouseDown: this._onMouseDown.bind(this)
        },
            this
        );
    },
    _onMouseDown: function (event) {
        var pos = event.getLocation();
        var column = Math.floor((pos.x - this.x) / 64);
        var row = Math.floor((pos.y - this.y) / 64);
        this._popCandy(column, row);
    },
    _popCandy: function (column, row) {
        var pool = [this.candySprite[column][row]];
        var index = 0;
        var addpool = function (candy) {
            if (pool.indexOf(candy) < 0)
                pool.push(candy);
        };
        while (index < pool.length) {
            var candy = pool[index];
            column = candy.column;
            row = candy.row;
            if (this._check(column + 1, row) && this.candySprite[column + 1][row].type == candy.type) {
                addpool(this.candySprite[column + 1][row]);
            }
            if (this._check(column - 1, row) && this.candySprite[column - 1][row].type == candy.type) {
                addpool(this.candySprite[column - 1][row]);
            }
            if (this._check(column, row + 1) && this.candySprite[column][row + 1].type == candy.type) {
                addpool(this.candySprite[column][row + 1]);
            }
            if (this._check(column, row - 1) && this.candySprite[column][row - 1].type == candy.type) {
                addpool(this.candySprite[column][row - 1]);
            }
            ++index;
        }
        if (index < 2) {
            return;
        }
        else {
            this.score += pool.length * pool.length;
            this.steps++;
            for (var i = 0; i < pool.length; ++i) {
                var candy = pool[i];
                this.candySprite[candy.column][candy.row] = null;
                this.removeChild(candy);
            }
            
            this._generateCandy();
        }
    },
    _generateCandy: function () {
        for (var i = 0; i < Constant.CANDY_MAXSIZE; ++i) {
            var misscount = 0;
            /*for(var j=0;j<this.candySprite[i].length;++j)
            {
                var candy = this.candySprite[i][j];
                if (!candy)
                {
                    var newCandy = Candy.createCandy(i,Constant.CANDY_MAXSIZE+misscount);
                    newCandy.x = newCandy.column * 64 + 32;
                    newCandy.y = newCandy.row * 64 + 32;
                    this.addChild(newCandy);
                    this.candySprite[i][newCandy.row] = newCandy;
                    misscount++;
                }
                else 
                {
                    if (misscount > 0)
                    {
                        var move = cc.moveTo(1, candy.x, candy.y - 64 * misscount);
                        candy.runAction(move);
                        candy.row -= misscount;
                        this.candySprite[i][j - misscount] = candy;
                        //this.candySprite[i][j] = null;                                          
                    }
                }
            }
            for (var j = this.candySprite[i].length; j >= 10; --j) {
                this.candySprite[i].splice(j, 1);
            }*/

            for (var j = 0; j < this.candySprite[i].length; ++j) {
                var candy = this.candySprite[i][j];
                if (candy) {
                    if (misscount > 0) {
                        var move = cc.moveTo(1, candy.x, candy.y - 64 * misscount);
                        candy.runAction(move);
                        candy.row -= misscount;
                        this.candySprite[i][candy.row] = candy;
                        this.candySprite[i][j] = null;
                    }
                }
                else {
                    misscount++;
                }
            }
            cc.log("i:" + i + " misscount:" + misscount);
            for (var j = 0; j < misscount; ++j) {
                var newCandy = Candy.createCandy(i, Constant.CANDY_MAXSIZE + j);
                newCandy.x = newCandy.column * 64 + 32;
                newCandy.y = newCandy.row * 64 + 32;
                this.addChild(newCandy);
                var move = cc.moveTo(1, newCandy.x, newCandy.y - 64 * misscount);
                newCandy.runAction(move);
                newCandy.row -= misscount;
                this.candySprite[i][newCandy.row] = newCandy;
                this.candySprite[i].splice(Constant.CANDY_MAXSIZE + j, 1);
            }
        }
    },
    _check: function (col, row) {
        if (col >= 0 && col < Constant.CANDY_MAXSIZE && row >= 0 && row < Constant.CANDY_MAXSIZE)
            return true;
        else
            return false;
    }
});